using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class FormSystemCode : FormBase
    {
        public FormSystemCode()
        {
            InitializeComponent();

            PopulateGroupList();
        }

        private void PopulateGroupList()
        {
            List<string> GroupList = doc_system.GetCodeGroupList();
            foreach (string Group in GroupList)
            {
                cboCodeGroup.Items.Add(Group);
            }
        }

        private void GetValueListForGroup(string group)
        {
            if (lsv_Code.Items.Count > 0)
                lsv_Code.Items.Clear();

            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht["CODE_GROUP"] = group;

            List<string> FieldList = new List<string>();
            FieldList.Add("CODE");
            FieldList.Add("DESC");

            FormMain.DocLib_QB.TableName = def.tb_System_Code;
            FormMain.DocLib_QB.Get_Select(FieldList, ht);

            System.Collections.ArrayList alist = FormMain.DocLib_QB.GetDataRow_ArrayList(false);

            foreach (object obj in alist)
            {
                if (obj is List<string>)
                {
                    List<string> aRow = obj as List<string>;
                    string code = aRow[0];
                    string desc = aRow[1];

                    ListViewItem aitem = new ListViewItem();
                    aitem.Text = code;
                    aitem.SubItems.Add(desc);
                    aitem.Tag = code;

                    lsv_Code.Items.Add(aitem);
                }
            }
        }

        private void SaveChangesForGroup()
        {
            wait_start();

            string group = cboCodeGroup.Text;

            foreach (ListViewItem aitem in lsv_Code.Items)
            {
                string code = aitem.Text;
                string desc = aitem.SubItems[1].Text;

                doc_system.SetCodeDescription(group, code, desc);                
            }

            wait_end();
        }

        private void cboCodeGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            string group = cboCodeGroup.Text;
            GetValueListForGroup(group);

            txtCode.Text = "";
            txtValue.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveChangesForGroup();
        }

        private void lsv_Code_Click(object sender, EventArgs e)
        {
            if (lsv_Code.SelectedItems.Count > 0)
            {
                ListViewItem aitem = lsv_Code.SelectedItems[0];
                string code = aitem.Text;
                string desc = aitem.SubItems[1].Text;

                txtCode.Text = code;
                txtValue.Text = desc;
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            string code = txtCode.Text;
            string desc = txtValue.Text;

            foreach (ListViewItem aitem in lsv_Code.Items)
            {
                if (aitem.Tag != null && aitem.Tag.ToString() == code)
                {
                    aitem.SubItems[1].Text = desc;
                    return;
                }
            }

            //if the item is not found, create a new item instead
            ListViewItem aNewItem = new ListViewItem();
            aNewItem.Text = code;
            aNewItem.Tag = code;
            aNewItem.SubItems.Add(desc);
            lsv_Code.Items.Add(aNewItem);
        }
    }
}